package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.entity.*;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MPartMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMPartService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: m_part
 * @Author: xiaoyq
 * @Date:   2023-09-11
 * @Version: V1.0
 */
@Service
public class MPartServiceImpl extends ServiceImpl<MPartMapper, MPart> implements IMPartService {

    @Resource
    private MPartLogServiceImpl logService;

    @Resource
    private MGlContractInformationServiceImpl informationService;

    public String receive(MPartLog partLog){
        MPart part = baseMapper.selectById(partLog.getPartId());

        if (partLog.getUseNum() <= 0){
            return "领取数量错误";
        }

        if (part==null){
            return "领用对象实例化错误";
        }

        if (part.getRemainder() < partLog.getUseNum()){
            return "剩余数量不足领用";
        }

        part.setRemainder(part.getRemainder() - partLog.getUseNum());
        part.setUseNum(part.getUseNum() + partLog.getUseNum());
        baseMapper.updateById(part);

        return "";
    }

    public void repay(String id){
        MPartLog partLog = logService.getById(id);

        MPart part = baseMapper.selectById(partLog.getPartId());
        part.setRemainder(part.getRemainder() + partLog.getUseNum());
        part.setUseNum(part.getUseNum() - partLog.getUseNum());
        baseMapper.updateById(part);
    }

    public void test() {

        QueryWrapper<MPart> queryWrapper = QueryGenerator.initQueryWrapper(new MPart(), new HashMap<>());
        List<MPart> list = baseMapper.selectList(queryWrapper);

        for (MPart part : list){

            MGlContractInformation contractInformation = informationService.getById(part.getConfiguration());
            MPart temp = baseMapper.getTemp(part.getPartName());
            if (temp==null){
                continue;
            }
            part.setContractId(contractInformation.getId());
            part.setContractName(contractInformation.getName());
            part.setPartName(temp.getPartName());
            part.setConfiguration(temp.getConfiguration());
            baseMapper.updateById(part);
        }


    }
}
